package com.wei.controller;

import com.wei.entity.Chapter;
import com.wei.entity.vo.ChapterVo;
import com.wei.service.ChapterService;
import com.wei.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wjx
 * @ClassName ChapterController
 * @description: TODO
 * @date 2022-11-29 21:14
 * @version: 1.0
 */
@RestController
@RequestMapping("/eduservice/chapter")
public class ChapterController {

    @Autowired
    private ChapterService chapterService;

    @GetMapping("/getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable String courseId) {

        List<ChapterVo> list = chapterService.getChapterVideoByCid(courseId);

        return R.ok().data("list", list);
    }

    /**
     * 添加章节
     *
     * @param chapter
     * @return
     */
    @PostMapping("/addChapter")
    public R addChapter(@RequestBody Chapter chapter) {

        boolean flag = chapterService.save(chapter);

        return flag ? R.ok() : R.error();
    }

    /**
     * 根据id查询章节信息
     *
     * @param id
     * @return
     */
    @GetMapping("/getChapterById/{id}")
    public R getChapterById(@PathVariable String id) {

        Chapter chapter = chapterService.getById(id);

        return R.ok().data("chapter", chapter);
    }

    /**
     * 更新章节信息
     *
     * @param chapter
     * @return
     */
    @PostMapping("/updateChapter")
    public R updateChapter(@RequestBody Chapter chapter) {

        boolean flag = chapterService.updateById(chapter);

        return flag ? R.ok() : R.error();
    }

    /**
     * 根据id删除章节
     *
     * @param id
     * @return
     */
    @DeleteMapping("/deleteChapterById/{id}")
    public R deleteChapterById(@PathVariable String id) {

        boolean flag = chapterService.deleteChapterById(id);

        return flag ? R.ok() : R.error();
    }

}
